import os,datetime
import shutil
def del_file(path):
    if os.path.exists(path):
        if os.path.isfile(path):
            os.remove(path)
            print("remove:", path)
        elif os.path.isdir(path):
            shutil.rmtree(path)
            print("rmtree:", path)
def del_nulldir(path):
    if os.path.exists(path):
        if os.path.isdir(path):
            if os.listdir(path)==[]:
                os.rmdir(path)
                print("rmdir:", path)
            else:
                for i in os.listdir(path):
                    del_nulldir(os.path.join(path,i))

def del_file_bysize(path,maxsize=None,minsize=None):
    """
    删除文件,根据大小删除
    maxsize: 最大大小
    minsize: 最小大小
    两个都设置,是居于中间的删除,一个设置,是居于一个方向的删除
    :param path:
    :param maxsize:
    :param minsize:
    :return:
    """
    if os.path.exists(path):
        if os.path.isfile(path):
            size=os.path.getsize(path)
            if maxsize is not None and minsize is not None:
                if size>minsize and size<maxsize:
                    os.remove(path)
                    print("remove:", path)
            elif maxsize is not None:
                if size>maxsize:
                    os.remove(path)
                    print("remove:", path)
            elif minsize is not None:
                if size<minsize:
                    os.remove(path)
                    print("remove:", path)
        else:
            for i in os.listdir(path):
                del_file_bysize(os.path.join(path,i),maxsize,minsize)
def del_file_bytime(path,maxdays=10):
    if os.path.exists(path):
        if os.path.isfile(path):
            time=os.path.getctime(path)
            time=datetime.datetime.fromtimestamp(time)
            now=datetime.datetime.now()
            if (now-time).days>maxdays:
                os.remove(path)
        else:
            for i in os.listdir(path):
                del_file_bytime(os.path.join(path,i),maxdays)
if __name__ == "__main__":
    del_file(r"G:\rq_future_tick_dfzip_20200102_20251114")